package com.ctesi.service;

import com.ctesi.mapper.BidMapper;
import com.ctesi.pojo.po.hubei.AgencyPO;
import com.ctesi.pojo.po.hubei.BidPO;
import com.ctesi.pojo.po.hubei.BidderPO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

//本类用于封装对象以及与dao层交互，上传数据到数据库。
@Service
public class zhaobiaopipelineService implements Pipeline {
    //注入所需要的实体类和插入数据库的方法
    @Autowired
    private BidMapper bidMapper;
    @Autowired
    private AgencyPO agencyPO;
    @Autowired
    private BidderPO bidderPO;
    @Autowired
    private BidPO bidPO;

    //对爬取到的数据封装到对象中，并调用接口写入数据库。
    @Override
    public void process(ResultItems resultItems, Task task) {

        //判断招标编号是否为空，为空则不要这段数据，不为空则写入数据库。
        if (resultItems.get("bidId") != null) {
            System.out.println("-------------------招标编号不为空，将数据写入各个数据库-----------------------");

            //封装id到对象属性中
            bidPO.setBidId(resultItems.get("bidId"));
            agencyPO.setBidId(resultItems.get("bidId"));
            bidderPO.setBidId(resultItems.get("bidId"));

            //封装招标单位信息
            bidderPO.setBidUnity(resultItems.get("bidUnity"));
            bidderPO.setBidAddress(resultItems.get("bidAddress"));
            bidderPO.setBidName(resultItems.get("bidName"));       //招标单位联系人
            bidderPO.setBidPhone(resultItems.get("bidPhone"));

            //封装代理机构信息
            agencyPO.setAgencyUnity(resultItems.get("agencyUnity"));   //代理机构单位
            agencyPO.setAgencyAddress(resultItems.get("agencyAddress"));
            agencyPO.setAgencyName(resultItems.get("agencyName"));  //代理机构联系人
            agencyPO.setAgencyPhone(resultItems.get("agencyPhone"));   //联系人电话

            //封装招标公告主体信息
            bidPO.setBidType(resultItems.get("bidType"));
            bidPO.setBidProvince(resultItems.get("bidProvince"));
            bidPO.setBidTitle(resultItems.get("bidTitle"));
            bidPO.setBidArange(resultItems.get("bidArange"));
            bidPO.setBidPubTime(resultItems.get("bidPubTime"));
            bidPO.setMemo(resultItems.get("memo"));
            bidPO.setBidUrl(resultItems.get("bidUrl"));
            bidPO.setBidArea(resultItems.get("bidArea"));
            bidPO.setBidCredit(resultItems.get("bidCredit"));
            bidPO.setBidJudgeMethod(resultItems.get("bidJudgeMethod"));
            bidPO.setBidStatus(resultItems.get("bidStatus"));
            bidPO.setDeadLine(resultItems.get("deadLine"));
            bidPO.setBidPrice(resultItems.get("bidPrice"));
            bidPO.setBidCondition(resultItems.get("bidCondition"));


            //将所有数据插入对应的数据库中
            bidMapper.insertMain(bidPO);
            bidMapper.insertAgency(agencyPO);
            bidMapper.insertBidder(bidderPO);

            //输出结果
            System.out.println("---------------------------写入数据库成功-------------------");
        } else {
            System.out.println("当前招标编号为空，不注入数据库。");
        }


    }
}
